<?php
require_once ('library/Model/BaseModel.php');

/**
 *
 * @author Nguyen
 *        
 */
class User extends BaseModel implements IModel {
	public function login($account, $pass) {
		$sql = 'select * from user where account = "' . $account . '" and password="' . iKey::encrypepass ( $pass ) . '"';
		$result = $this->db->fetchAll( $sql );
		if(count($result)>0){
			return $result[0];
		}
		return null;
	}
	/*
	 * (non-PHPdoc) @see BaseModel::getall()
	 */
	public function getall($strlimit = NULL) {
		// TODO Auto-generated method stub
		$sql = 'select u.id,u.code,u.username,u.numberphone,u.address,u.email,u.image,p.name as powername,c.name as companyname
				from user as u  left join userpower as p on u.userpower = p.id left join company as c on u.company = c.id 
				order by u.id ASC '.$strlimit;
		return $this->db->fetchAll ( $sql );
	}
	/*
	 * (non-PHPdoc) @see BaseModel::getbyid()
	 */
	public function getbyid(array $id) {
		// TODO Auto-generated method stub
		$b = sizeof ( $id );
		$c = 0;
		$sql = 'select u.id,u.username,u.numberphone,u.address,u.email,u.image,p.name as powername,c.name as companyname
				from user as u  left join userpower as p on u.userpower = p.id left join company as c on u.company = c.id where ';
		foreach ( $id as $key => $value ) {
			$sql .= 'u.'.$key . '=' . $value;
			if ($c < $b - 1) {
				$sql .= ' and ';
				$c += 1;
			}
		}
		$entity = $this->db->fetchRow ( $sql );
		return $entity;
	}
	/* (non-PHPdoc)
	 * @see BaseModel::insert()
	 */
	public function insert(array $entity) {
		// TODO Auto-generated method stub
		$entity['code'] = $this->generateCode($entity['company']);
		$entity['password'] = iKey::encrypepass($entity['password']);
		$sql = "insert into user (";
		$c = $b = 0;
		foreach ( $entity as $key => $value ) {
				
			if ($c == 0) {
				$sql .= $key;
				$c = 1;
			} else
				$sql .= "," . $key;
		}
		$sql .= ")";
		$sql .= " values (";
		foreach ( $entity as $key => $value ) {
			if ($b == 0) {
				$sql .= "'" . $value . "'";
				$b = 1;
			} else
				$sql .= "," . "'" . $value . "'";
		}
		$sql .= ")";
		return $this->db->execute ( $sql );
	}

	/* (non-PHPdoc)
	 * @see BaseModel::update()
	 */
	public function update(array $entity) {
		// TODO Auto-generated method stub
		$id = $entity ['id'];
		$old = $this->getbyid(array('id'=>$entity ['id']));
		if ($old['company'] != $entity['company']) {
			$entity['code'] = $this->generateCode($entity['company']);
		}
		$entity['password'] = iKey::encrypepass($entity['password']);
		iDebug::debugArray($entity);
		$entity = iArray::removeAtTop ( $entity, 1 );
		$b = sizeof ( $entity );
		$c = 0;
		$sql = "update user set ";
		foreach ( $entity as $key => $value ) {
			$c += 1;
			if ($b == $c)
				$sql .= $key . " = " . "'" . $value . "'";
			else
				$sql .= $key . " = " . "'" . $value . "'" . ",";
		}
		$sql .= " where id = " . $id;
		echo $sql;
		return $this->db->execute ( $sql );
	}
	/* (non-PHPdoc)
	 * @see BaseModel::generateCode()
	 */
	public function generateCode($key) {
		// TODO Auto-generated method stub
		$sql = 'select code from company where id = '.$key;
		$code = $this->db->fetchRow($sql);
		$code = $code["code"].'U';
		$sql1 = 'select code from user where company = '.$key.' order by code DESC limit 0,1';
		$x = $this->db->fetchRow($sql1);
		$x = intval(substr($x['code'], 4, strlen($x['code']))) + 1;
		if ($x < 10)
			return $code = $code.'0'.$x;
		return $code = $code.$x;
	}
}
?>